package cn.yanyong.org.controller;

import cn.yanyong.org.basic.util.AjaxResult;
import cn.yanyong.org.basic.util.PageList;
import cn.yanyong.org.domain.Employee;
import cn.yanyong.org.query.EmployeeQuery;
import cn.yanyong.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 基于id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> queryAll() {
        return employeeService.queryAll();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！！");
        }

    }

    /**
     * 高级查询
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！！");
        }

    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            employeeService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！！");
        }

    }

}
